package com.yaowk.utils.generator;

import lombok.Data;

import java.util.Collection;
import java.util.List;

@Data
public abstract class AbstractGenerator implements IGeneratorTemplate {

    private String packageTemplate = "package %s;%n%n";  // 包名模板
    private String importTemplate = "import %s;%n"; // 引入模板
    private String entityPrefix = ""; // entity前缀
    private String packageName; // 包名
    private List<Class> entities; // entity列表
    private String basePath;
    private String prefix = "";
    private String suffix = "";

    /**
     * 生成包名
     *
     * @param stringBuilder
     * @return
     */
    public StringBuilder appendPackage(StringBuilder stringBuilder) {
        stringBuilder.append(String.format(packageTemplate, packageName));
        return stringBuilder;
    }

    /**
     * 生成引用
     *
     * @param stringBuilder
     * @param imports
     * @return
     */
    public StringBuilder appendImport(StringBuilder stringBuilder, Collection<String> imports) {
        for (String clazz : imports) {
            stringBuilder.append(String.format(importTemplate, clazz));
        }
        stringBuilder.append("\n");
        return stringBuilder;
    }

    /**
     * 生成类名
     *
     * @param clazz
     * @return
     */
    @Override
    public String buildClassName(Class clazz) {
        String className = prefix + clazz.getSimpleName().replaceFirst(getEntityPrefix(), "") + suffix;
        return className;
    }

    /**
     * 生成类类型
     *
     * @param clazz
     * @return
     */
    @Override
    public String buildClassType(Class clazz) {
        String className = buildClassName(clazz);
        return packageName + "." + className;
    }
}
